package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.pojo.Coupon;
import com.gxa.autoparts.core.pojo.GoodsSku;
import com.gxa.autoparts.portal.service.CheckMyCouponService;
import com.gxa.autoparts.portal.service.GoodsSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Transactional(rollbackFor = Throwable.class)
@RequestMapping("/myCoupon")
@Api(tags = "我的优惠券")
public class MyCouponController {
    @Autowired
    CheckMyCouponService checkMyCouponService;

    @Autowired
    GoodsSkuService goodsSkuService;


    @PostMapping("/checkMyCoupon")
    @ApiOperation("查看我的各类优惠券（未使用，已使用，已过期）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long",paramType = "query")
    })
    public Map checkMyCoupon(Long id){
        List<Coupon> usedCouponList = checkMyCouponService.usedCouponList(id);
        List<Coupon> unusedCouponList = checkMyCouponService.unusedCouponList(id);
        List<Coupon> expiredCouponList = checkMyCouponService.expiredCouponList(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("usedCouponList",usedCouponList);
        jsonResult.put("unusedCouponList",unusedCouponList);
        jsonResult.put("expiredCouponList",expiredCouponList);
        return jsonResult.getDataMap();

    }

    /**
     * 领券中心获取所有的券
     * 杨述波
     * @return
     */
    @PostMapping("/unReceivedCoupon")
    @ApiOperation("领券中心查看优惠券（未领取，已领取）")
    public Map unReceivedCoupon(){
        Long id =1L;
        Map<String, Object> couponInfoByUserId = checkMyCouponService.getCouponInfoByUserId(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(couponInfoByUserId);
        return jsonResult.getDataMap();

    }

    @PostMapping("/useCoupon")
    @ApiOperation("下单的时候查询可用优惠券(可使用，未满金额不可用，已过期，已使用),需要json字符串(数量和skuId)")
    public Map useCoupon(@RequestBody Map map){
        Long userId = Long.parseLong(map.get("userId").toString());
        List<Coupon> usedCouponList = checkMyCouponService.usedCouponList(userId);
        List<Coupon> expiredCouponList = checkMyCouponService.expiredCouponList(userId);
        List<Coupon> unUsedCouponList = checkMyCouponService.unusedCouponList(userId);
        List<Coupon> canNotUsedCouponList = new ArrayList<>();
        List<Coupon> canUseCouponList = new ArrayList<>();
        List skuWithNumber = (List)map.get("skuWithNumber");
        Double total = 0D;
        for (Object o : skuWithNumber) {
            Long skuId = Long.parseLong(((Map) o).get("skuId").toString());
            Long number = Long.parseLong(((Map) o).get("number").toString());
            GoodsSku goodsSku = goodsSkuService.selectById(skuId);
            total += goodsSku.getPrice()*number;
        }
        for (Coupon coupon : unUsedCouponList) {
            String name = coupon.getName();
            String[] s1 = name.split("满");
            String[] s2 = s1[1].split("减");
            String s = s2[0];
            Double money = Double.parseDouble(s);
            if (total >= money){
                canUseCouponList.add(coupon);
            }else {
                canNotUsedCouponList.add(coupon);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("usedCouponList",usedCouponList);
        jsonResult.put("expiredCouponList",expiredCouponList);
        jsonResult.put("canNotUsedCouponList",canNotUsedCouponList);
        jsonResult.put("canUseCouponList",canUseCouponList);
        return jsonResult.getDataMap();
    }
}
